﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class LevelClickLove : MonoBehaviour
{
    public List<Transform> loveRootList = new List<Transform>();
    public List<TextButton> tbList = new List<TextButton>();
    private List<int> fadeIndexQueue = new List<int>();
    private Dictionary<int, Transform> loveTsfDic = new Dictionary<int, Transform>();
    private string curStr = string.Empty;
    private string targetStr = "LOVE";
    public GameLevelItem gameLevelItem;
    private bool isWin = false;

    // Start is called before the first frame update
    void Start()
    {
        InitTBList();
    }

    private void InitTBList()
    {
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            tbList[i].Init(tbList[tempIndex].text.text, () =>
            {
                if (isWin) return;
                tbList[tempIndex].text.DOFade(0, 0.5f);
                fadeIndexQueue.Add(tempIndex);
                curStr += tbList[tempIndex].text.text.Trim();
                JudgeResult();
            });
        }
    }

    private void JudgeResult()
    {
        Debug.Log(string.Format("curStr: {0}, targetStr :{1}", curStr, targetStr));
        if (curStr.Equals(targetStr))
        {
            isWin = true;
            InitLoveDic();
            ShowLove();
        }
    }

    private void InitLoveDic()
    {
        loveTsfDic.Clear();
        int count = fadeIndexQueue.Count;
        for (int i = 0; i < count; i++)
        {
            if (tbList[fadeIndexQueue[i]].text.text.Trim().Equals("L"))
            {
                loveTsfDic.Add(0, tbList[fadeIndexQueue[i]].text.transform);
                Debug.Log("add L");
            }
            else if (tbList[fadeIndexQueue[i]].text.text.Trim().Equals("O"))
            {
                loveTsfDic.Add(1, tbList[fadeIndexQueue[i]].text.transform);
                Debug.Log("add O");
            }
            else if (tbList[fadeIndexQueue[i]].text.text.Trim().Equals("V"))
            {
                loveTsfDic.Add(2, tbList[fadeIndexQueue[i]].text.transform);
                Debug.Log("add V");
            }
            else if (tbList[fadeIndexQueue[i]].text.text.Trim().Equals("E"))
            {
                loveTsfDic.Add(3, tbList[fadeIndexQueue[i]].text.transform);
                Debug.Log("add E");
            }
        }
    }

    private void ShowLove()
    {
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            if (fadeIndexQueue.Contains(i))
                tbList[i].text.DOFade(1, 0.5f);
            else
                tbList[i].text.DOFade(0, 0.5f);
        }
        Invoke("WinAni", 0.8f);
    }

    private void WinAni()
    {
        int count = loveRootList.Count;
        Debug.Log("LOVE TSF DIC COUNT :" + loveTsfDic.Count);
        for (int i = 0; i < count; i++)
        {
            loveTsfDic[i].parent = this.transform;
            loveTsfDic[i].DOLocalMove(loveRootList[i].localPosition, 0.5f);
        }
        Invoke("TriggerResult", 0.8f);
    }

    private void TriggerResult()
    {
        gameLevelItem.TriggerResult(true);
    }


}
